<?php
class admin_module_spa
{
    private $template;
    private $util;
    private $db;
    private $content;
    private $module;
    private $id;
    
    public function __construct(){ 
        require_once("../code/db.class.php");
        require_once("../code/pagination.class.php");   
        $this->util = new util();
        $this->db = new db();
    }
    
    public function autorun(){
        $act = isset($_GET['act'])?$_GET['act']:'about';
        switch($act){
            case 'about':
                $this->about();
                break;
            case 'image':
                $this->image();
                break;
            case 'order':
                $this->orders();
                break;
            case 'guestbook':
                $this->guestbook();
                break;
            default:
                $this->get_index();
                break;
        }     
    } 
    //get index of spa
    private function get_index(){
        $this->util->send_var('module','layout');
        $this->util->generate_page(ADMIN_TEMPLATES.'/spa/index.tpl');  
    } 
    //About
    private function about(){
        $code = isset($_GET['code'])?$_GET['code']:'view';
         switch($code){
            case 'view':
                $this->view_about();
                break;
            case 'edit':
                $this->edit_about();
                break;
            case 'doupdate':
                $this->do_update_about();
                break;
            default:
                $this->view_about();
                break;
        }     
    } 
    //Image
    private function image(){
        $code = isset($_GET['code'])?$_GET['code']:'list';
         switch($code){
            case 'add':
                $this->add_image();
                break;
            case 'docreate':
                $this->do_create_image();
                break;
            case 'edit':
               
                break;
            case 'list':
                 $this->get_image_list();  
                break;
            default:
                $this->get_image_list();
                break;
        }     
    } 
  //Orders
    private function orders(){
        $code = isset($_GET['code'])?$_GET['code']:'list';
         switch($code){
            case 'list':
                $this->list_orders();
                break;
            case 'paging':
                $this->get_paging_image();
                break;
            case 'edit':
                $this->edit_order();
                break;
            case 'doupdate':
                $this->do_update_order();
                break;
            default:
                $this->list_orders();
                break;
        }     
    } 
    
    //---------------------------------------------------------------
    private function get_image_list(){
        $filter = isset($_GET['filter'])?strval($_GET['filter']):'all'; 
        switch ($filter)
        {
            case 'all':
                $str = "SELECT * FROM images WHERE site='3'"; 
                break;
            case '1':
                $str = "SELECT * FROM images WHERE site='3' and ishow='1'"; 
                break;
            case '0':
                $str = "SELECT * FROM images WHERE site='3' and ishow='0'"; 
                break;
            default:
                $str = "SELECT * FROM images WHERE site='3'"; 
                break;
                
        }
        $this->db->openconnection();
        $data = $this->db->run_query($str);
        $this->db->closeconnection();
        $pagination = new pagination;
        $imagePages = $pagination->generate($data,2);
        //Send data to template
        $this->module = 'list_image';
        $this->util->send_var('module',$this->module);
        $this->util->send_var('data',$imagePages);
        $this->util->send_var('pagination',$pagination->links()); 
        $this->util->generate_page(ADMIN_TEMPLATES.'/spa/index.tpl');      
    }
     private function get_paging_image(){
        $filter = isset($_GET['filter'])?strval($_GET['filter']):'all'; 
        switch ($filter)
        {
            case 'all':
                $str = "SELECT * FROM images WHERE site='3'"; 
                break;
            case '1':
                $str = "SELECT * FROM images WHERE site='3' and ishow='1'"; 
                break;
            case '0':
                $str = "SELECT * FROM images WHERE site='3' and ishow='0'"; 
                break;
            default:
                $str = "SELECT * FROM images WHERE site='3'"; 
                break;
                
        }
        $this->db->openconnection();
        $data = $this->db->run_query($str);
        $this->db->closeconnection();
       ///------------------------------
        // Create the pagination object
        $pagination = new pagination;
        $imagePages = $pagination->generate($data,2);
        
        //-------------------------------------------  
        $this->util->send_var('data',$imagePages);  
        $this->util->send_var('pagination',$pagination->links()); 
       
        if($this->act=='paging'){
            $pageCurr =  isset($_GET['page'])?$_GET['page']:1;
            $startNum = 2*($pageCurr-1) + 1;
            $this->module = 'list_image';
            $this->util->send_var('currNum',$startNum); 
            $this->util->generate_page(ADMIN_TEMPLATES.'/spa/index.tpl');
        }
        else
        { 
            $this->module = 'list_image';
            $this->util->generate_page(ADMIN_TEMPLATES.'/spa/index.tpl'); 
        }    
    }
    private function add_image(){
        //Send data to template
        $this->module = 'create_image';
        $this->util->send_var('module',$this->module);
        $this->util->generate_page(ADMIN_TEMPLATES.'/spa/index.tpl');      
    }
    
     //Do Create new image
    private function do_create_image(){
        //take input
        $image_status = isset($_POST['image_status'])?1:0;
        //Upload file
        $image = $this->util->uploader('image','images/about/');
        if(($image['error']==FILE_EXISTS) || (($image['error']==0))){
            $avatar = $image['name'];    
        }
        else
        {
            $avatar = '';    
        }
        //Insert database
        $str = "INSERT INTO images(image,site,ishow) VALUES ('$avatar', 3, '$image_status')";        
        $this->db->openconnection();
        $this->db->run_query($str);
        $this->db->closeconnection();
        //Oke
        $this->get_image_list();        
    }
    //--------------------------------------------------------------------------------------
       //---------------------------------------------------------------
    private function list_orders(){
        $filter = isset($_GET['filter'])?strval($_GET['filter']):'all'; 
        switch ($filter)
        {
            case 'all':
                $str = "SELECT * FROM `order` WHERE site='3' ORDER BY id DESC"; 
                break;
            case '1':
                $str = "SELECT * FROM `order` WHERE site='3' and ishow='1' ORDER BY id DESC"; 
                break;
            case '0':
                $str = "SELECT * FROM `order` WHERE site='3' and ishow='0' ORDER BY id DESC"; 
                break;
            default:
                $str = "SELECT * FROM `order` ORDER BY id DESC"; 
                break;
                
        }
        $this->db->openconnection();
        $data = $this->db->run_query($str);
        $this->db->closeconnection();
        //Send data to template
        $this->module = 'list_orders';
        $this->util->send_var('module',$this->module);
        $this->util->send_var('data',$data);
        $this->util->generate_page(ADMIN_TEMPLATES.'/spa/index.tpl');      
    }
    //-------------------------------------------------------------------------------------
     private function view_about(){
        $sql = "SELECT * FROM about WHERE site='3' and ishow='1'";
        $this->db->openconnection();
        $about = $this->db->run_query($sql);
        $this->db->closeconnection();
        $lang = isset($_GET['lang'])?strval($_GET['lang']):'en';  
        
        $this->util->send_var('about',$about[0]); 
        $this->util->send_var('language',$lang);
        $this->module = 'view_about';  
        $this->util->send_var('module',$this->module);
        $this->util->generate_page(ADMIN_TEMPLATES.'/spa/index.tpl');   
     }
      //Load template to update artist
    private function edit_about(){
        $sql = "SELECT * FROM about WHERE site='3' and ishow='1'";
        $this->db->openconnection();
        $data = $this->db->run_query($sql);
        $this->db->closeconnection();
        $lang = isset($_GET['lang'])?strval($_GET['lang']):'en';   
        
        switch($lang){
            case 'en':
                $editor = $this->util->create_editor('description',$data[0]['endescription']);  
                $title = $data[0][entitle];
                break;    
            case 'fr':
                $editor = $this->util->create_editor('description',$data[0]['frdescription']);  
                $title = $data[0][frtitle];
                break;
            case 'vn':
                $editor = $this->util->create_editor('description',$data[0]['vndescription']);  
                $title = $data[0][vntitle];
                break;
        }
        
        //Send data to template
        $this->module = 'update_about';
        $this->util->send_var('module',$this->module);
        $this->util->send_var('editor',$editor);
        $this->util->send_var('language',$lang); 
        $this->util->send_var('title',$title);
        $this->util->send_var('id',$data[0]['id']); 
        $this->util->generate_page(ADMIN_TEMPLATES.'/spa/index.tpl');    
    }
    
     //Do update artist
    private function do_update_about(){
        $id = isset($_GET['id'])?$_GET['id']:0;
        if($id==0){
            //Lỗi: không có giá trị 0 trong csdl
            exit();    
        }
        //take input
        $title = isset($_POST['title'])?$_POST['title']:'';
        //$artist_avatar = isset($_POST['artist_avatar'])?$_POST['artist_avatar']:0; 
        $des = isset($_POST['description'])?strval($_POST['description']):'';
        $lang = isset($_GET['lang'])?strval($_GET['lang']):'en';   
        
        switch($lang){
            case 'en':
                $str = "UPDATE about SET entitle = '$title', endescription = '$des', ishow = '1' WHERE id= $id";     
                break;    
            case 'fr':                                                                                     
                $str = "UPDATE about SET frtitle = '$title', frdescription = '$des', ishow = '1' WHERE id= $id";     
                break;
            case 'vn':
                $str = "UPDATE about SET vntitle = '$title', vndescription = '$des', ishow = '1' WHERE id= $id";     
                break;
        }
        
        //Insert database
             
        $this->db->openconnection();
        $this->db->run_query($str);
        $this->db->closeconnection();
        //Oke
        $this->view_about();           
    }
}
?>
